﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using KhachSan.Process.Public;
using DevExpress.XtraEditors;
using KhachSan.Process.Private.Bo.Class;
namespace KhachSan.Process.Private.Bo
{
    public partial class UC_SoDoPhongChiTiet : UserControl
    {
      
        private int KHUNG_TRAI_WIDTH = 212;
        private const int KHU_WIDTH = 212;
        private const int TANG_WIDTH = 200;
        private const int TANG_HEIGHT= 50;
        private const int KHU_HEIGHT = 50;
        private  int PHONG_WIDHT = 100;
        private  int PHONG_HEIGHT = 100;
        private int loop_x = 5;

        private DataTable khu,tang,phong;
        private FlowLayoutPanel currenKhu;
        private F_ThongTinPhong thongTinPhong;
        private List<int> mau;
            
        enum TrangThaiPhong : int { dangTrong = 1, daCoKhachDat = 2, coKhach = 3, dangDon = 4, dangBaoTri = 5, khongBan = 6, phongDo = 7 };

        public UC_SoDoPhongChiTiet()
        {

          
            InitializeComponent();
        }
        private void bntLoadTang_Click(object sender)
        {
            HienTang(sender);
        }
        private void tang_Click(object sender)
        {
            LoadPhong(sender);
        }
        private void Phong_Leave(UC_Phong phong)
        {
            //thongTinPhong.Hide();
        }
        
        private void Phong_Hover(Class.Phong phong)
        {
            try
            {
                if (thongTinPhong != null)
                {
                    thongTinPhong.Show();
                    thongTinPhong.BringToFront();
                    Point c = System.Windows.Forms.Cursor.Position;
                    Point p = this.PointToClient(c);
                    thongTinPhong._LoaiPhong.Text = phong.GiaPhong1.HangPhong._LoaiPhong.LoaiPH;
                    thongTinPhong._KieuPhong.Text = phong.GiaPhong1.HangPhong._KieuPhong.DienGiai;
                    thongTinPhong._Gia1Nguoi.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.Gia1Khach);
                    thongTinPhong._Gia2Nguoi.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.Gia2Khach);
                    thongTinPhong._Gia3Nguoi.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.Gia3Khach);
                    thongTinPhong._Gia4Nguoi.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.Gia4Khach);
                    thongTinPhong._GiaThemNguoiLon.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.GiaThemNguoiLon);
                    thongTinPhong._GiaThemTreEm.Text = Class.Tien.setTextToCurrency(phong.GiaPhong1.GiaThemTreEm);

                    thongTinPhong.SetBounds(p.X, p.Y, thongTinPhong.Width, thongTinPhong.Height);
                }
            }
            catch { }
        }
        private void LoadPhong(object sender)
        {
            try
            {
                string maTang = ((FlatButton)sender).Tag.ToString();
                phong = XLDuLieu.LoadDataTable("sp_LayThongTinSoDoPhong "+maTang);
                panelPhong.Controls.Clear();
                string trangThaiPhong;
                int magin = 6;
                if (phong != null)
                {
                    for (int i = 0; i < phong.Rows.Count; i++)
                    {
                        DataRow r = phong.Rows[i];
                        UC_Phong uc_Phong = new UC_Phong();
                        Class.Phong objPhong= new Class.Phong(r["ID_Tang"].ToString()
                                                    , r["TenTang"].ToString()
                                                    , r["MaTTP"].ToString()
                                                    , r["TrangThai"].ToString()
                                                    ,(int) r["Mau"]
                                                    , r["MaPH"].ToString()
                                                    , r["MaGia"].ToString()
                                                    , r["DienGiaiGiaPhong"].ToString()
                                                    , r["TuNgay"].ToString()
                                                    , r["DenNgay"].ToString()
                                                    , (int)r["Gia1Khach"]
                                                    , (int)r["Gia2Khach"]
                                                    , (int)r["Gia3Khach"]
                                                    , (int)r["Gia4Khach"]
                                                    , (int)r["GiaThemNGLon"]
                                                    , (int)r["GiaThemTreEm"]
                                                    , r["MaLP"].ToString()
                                                    , r["LoaiPH"].ToString()
                                                    , r["MaKP"].ToString()
                                                    , r["DienGiaiKieuPhong"].ToString()
                                                    , r["MaHangPhong"].ToString()
                                               );
                        uc_Phong._Phong = objPhong;
                        uc_Phong._TieuDe.Text = objPhong.MaPH;
                        trangThaiPhong = objPhong.TrangThai1.MaTTP;
                        PHONG_WIDHT = PHONG_HEIGHT = (panelPhong.Width - magin * loop_x) / loop_x;
                        uc_Phong.di = new UC_Phong.leave(Phong_Leave);
                        uc_Phong.hover = new UC_Phong.Hover(Phong_Hover);
                        uc_Phong._TrangThaiPhong.BackColor = Color.FromArgb(objPhong.TrangThai1.Mau);
                        uc_Phong.Width = PHONG_WIDHT;
                        uc_Phong.Height = PHONG_HEIGHT;
                        if (trangThaiPhong == ((int)TrangThaiPhong.coKhach).ToString())
                        {
                           // uc_Phong._light.Image = Image.FromFile(Application.StartupPath + "/hinh_bo/yellow_light.png");
                        }
                        else
                        {

                        }
                        panelPhong.Controls.Add(uc_Phong);

                    }
                }
            }
            catch { }
        }

        void tao_day(int n, int m)
        {
            mau = new List<int>();
            Random r = new Random();
            for (int i = 0; i < n; i++)
            {
                mau.Add(i);
            }
            for (int i = 0; i < m; i++)
            {
                int cs = r.Next(i, n);
                int temp = mau[i];
                mau[i] = mau[cs];
                mau[cs] = temp;
            }

        }
        private void HienTang(object sender)
        {
            try
            {
                string maKhu = ((FlatButton)sender).Tag.ToString();
                tang = XLDuLieu.LoadDataTable(string.Format("Select * from TANG where MaKV=N'{0}'", maKhu));
                if (panelKhu.Controls.Contains(currenKhu))
                    panelKhu.Controls.Remove(currenKhu);

                if (tang != null)
                {
                    currenKhu = new FlowLayoutPanel();
                    currenKhu.Width = KHUNG_TRAI_WIDTH;
                    tao_day(1000, tang.Rows.Count);
                    currenKhu.Height = 500;
                    panelKhu.Controls.Add(currenKhu);
                    for (int i = 0; i < tang.Rows.Count; i++)
                    {
                        FlatButton nut = new FlatButton();
                        ButtonStyle(nut);
                        nut.Mau = mau[i];

                       // nut.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.UltraFlat;
                        nut.Tag = tang.Rows[i]["ID_Tang"];
                        nut.flat_Click=new FlatButton.Flat_Click(tang_Click);
                        nut.Text.Text = tang.Rows[i]["TenTang"].ToString();
                        nut.Size = new System.Drawing.Size(TANG_WIDTH, TANG_HEIGHT);
                        currenKhu.Controls.Add(nut);
                    }
                    panelKhu.Controls.Add(currenKhu);
                }
            }
            catch { }
        }
        void ButtonStyle(FlatButton button)
        {
            //button.Appearance.BackColor = System.Drawing.Color.SlateGray;
            //button.Appearance.BackColor2 = System.Drawing.Color.Ivory;
            //button.Appearance.BorderColor = System.Drawing.Color.Blue;
           // button.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.Flat;
            
            button.Font=  new System.Drawing.Font("Times New Roman", 10F);
            

            //button.Appearance.GradientMode = System.Drawing.Drawing2D.LinearGradientMode.Vertical;
            //button.Appearance.Options.UseBackColor = true;
           
            button.Location = new System.Drawing.Point(3, 3);
           // button.LookAndFeel.Style = DevExpress.LookAndFeel.LookAndFeelStyle.Flat;
            //button.LookAndFeel.UseDefaultLookAndFeel = false;
        }
        private void SoDoPhong_Load(object sender, EventArgs e)
        {
            try
            {
                this.panelKhu.Width = KHUNG_TRAI_WIDTH;
                khu = XLDuLieu.LoadDataTable("select * from KhuVuc");
                tao_day(1000, khu.Rows.Count);

                for (int i = 0; i < khu.Rows.Count; i++)
                {
                    FlatButton nut = new FlatButton();
                    ButtonStyle(nut);
                    nut.Mau = mau[i];
                    //nut.BorderStyle = DevExpress.XtraEditors.Controls.BorderStyles.UltraFlat;
                    nut.Text.Text = khu.Rows[i]["DienGiai"].ToString();
                    nut.Tag = khu.Rows[i]["MaKV"];
                    nut.flat_Click=new FlatButton.Flat_Click(bntLoadTang_Click);
                    nut.Size = new System.Drawing.Size(KHU_WIDTH, KHU_HEIGHT);
                    panelKhu.Controls.Add(nut);
                }

                thongTinPhong = new F_ThongTinPhong();

            }
            catch(Exception ex)
            { 
            
            }
        }



    }
}
